package org.duoyi.striver.service.thread;

import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.duoyi.striver.gui.controller.SideBarController;
import org.duoyi.striver.model.impl.Sleeper;
import org.duoyi.striver.service.common.CommonService;

@Slf4j
public class WindowsFollower extends Thread {

    private static boolean STATE = true;

    public WindowsFollower() {
        super("跟随线程");
    }

    public static void running() {
        STATE = true;
    }
    public static void stopping() {
        STATE = false;
    }


    @SneakyThrows
    @Override
    public void run() {
        log.debug("{}启动", this.getName());
        int hwnd = CommonService.bindWindows();
        WinDef.RECT rect = new WinDef.RECT();
        while (STATE) {
            boolean flag = User32.INSTANCE.GetWindowRect(new WinDef.HWND(new Pointer(hwnd)), rect);
            if (flag) {
                SideBarController.getInstance().moveTo(rect.right - 12, rect.top);
            }
            Sleeper.sleep(5000);
        }
        CommonService.release();
        log.debug("{}结束", this.getName());
    }
}
